package com.rural.bus;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.rural.bus.model.Order;
import com.rural.bus.model.Railway;
import com.rural.bus.net.OnNetCallback;
import com.rural.bus.utils.CallFailDialog;
import com.rural.bus.utils.ProgressUtil;
import com.rural.bus.view.CommonAlertDialog;
import com.rural.bus.view.CommonAlertDialog.OnDialogDismissListener;

/***
 * @author majie
 *
 */
public class WaitActivity extends BaseActivity {
	
	private TextView mTvCount;
//	private List<Order> mOrders=new ArrayList<Order>();
//	private XListView mXlv;
//	private SelectCarAdapter mAdapter;
	/***
	 * 倒计时标志位
	 */
	private boolean isContinue=true;
	private final int WAITTIME=300;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selectcar);
		
		initView();
		setListeners();
		initData();
		
	}

	@Override
	protected void initView() {
		mBtMiddle.setText("等待司机应答");
		mBtMiddle.setVisibility(View.VISIBLE);
		mBtMiddle.setTextColor(getResources().getColor(R.color.white));
		
		mBtLeft.setVisibility(View.VISIBLE);
		mBtLeft.setText("取消");
		
		mTvCount=(TextView) findViewById(R.id.tv_count);
//		mXlv=(XListView) findViewById(R.id.xlv);
	}

	@Override
	protected void initData() {
		count();
//		mAdapter=new SelectCarAdapter(this, mOrders);
//		mXlv.setAdapter(mAdapter);
	}

	@Override
	protected void setListeners() {
		mBtLeft.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View arg0) {
					CommonAlertDialog dialog = new CommonAlertDialog(WaitActivity.this, "确定", "取消", "您确认取消订单吗？", new OnDialogDismissListener() {

						@Override
						public void onConfirmClicked() {
							cancel();
						}

						@Override
						public void onCancelClicked() {

						}
					});
					dialog.show();
				}
			});
	}
	
	/***
	 * count thread
	 */
	private void count(){
		new Thread(){
			public void run() {
				int i=0;
				while(i<WAITTIME&&isContinue){
					i++;
					mHandler.sendEmptyMessage(i);
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			};
			
		}.start();
	}
	
	private Handler mHandler=new Handler(){
//		private WeakReference<WaitActivity> reference;
		
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			mTvCount.setText(""+msg.what);
			if(msg.what==WAITTIME){
				CallFailDialog.dialog(WaitActivity.this);
			}
		};
	};
	
    private void cancel(){
    	ProgressUtil.showProgress(this, "");
        Railway.cancel(mCurrentUser.userId, mCurrentUser.currentRailway.order.orderId, "", new OnNetCallback() {
			
			@Override
			public void success(JSONObject object) {
				Log.d("waitactivity", object.toString());
				ProgressUtil.closeProgress();
				finish();
			}
			
			@Override
			public void fail(String error) {
				ProgressUtil.closeProgress();
				toast(error);
			}
		});
    }
	
	public boolean onKeyDown(int keyCode, android.view.KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			return false;
		}
		return super.onKeyDown(keyCode, event);
	};
	
	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		Order order=(Order) intent.getSerializableExtra("order");
		if(order!=null){
			toast("receive success");
			mTvCount.setVisibility(View.GONE);
			isContinue=false;
//			mOrders.add(order);
//			mAdapter.notifyDataSetChanged();
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		isContinue=false;
	}

}
